import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header


def send_email(from_addr, password, to_addr, subject, content, attachment_files=[]):
    msg = MIMEMultipart()  # 创建一个MIMEMultipart对象
    msg['From'] = Header(from_addr)  # 设置发件人
    msg['To'] = Header(to_addr)  # 设置收件人
    msg['Subject'] = Header(subject, 'utf-8')  # 设置邮件主题
    msg.attach(MIMEText(content, 'html', 'utf-8'))  # 添加正文
    for file in attachment_files:  # 添加附件
        att = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8')  # 创建一个MIMEText对象
        att["Content-Type"] = 'application/octet-stream'  # 设置邮件内容类型为application/octet-stream
        att["Content-Disposition"] = 'attachment; filename="{}"'.format(file)  # 设置邮件附件
        msg.attach(att)  # 附加邮件
    try:  # 发送邮件
        smtp_server = 'smtp.qq.com'
        with smtplib.SMTP_SSL(smtp_server, 465) as smtpobj:
            smtpobj.login(from_addr, password)
            smtpobj.sendmail(from_addr, to_addr, msg.as_string())
            print("邮件发送成功")
    except Exception as e:
        print("⽆法发送邮件:", e)


if __name__ == '__main__':
    from_addr = '1228912646@qq.com'
    password = 'puxutzlcndajijea'
    to_addr = '1228912646@qq.com'
    subject = 'Python SMTP 邮件测试'
    html_content = """
    <p>Python 邮件发送HTML格式⽂件测试...</p>
    <p><a href="http://www.runoob.com">这是⼀个链接</a></p>
    """
    attachment_files = ['test1.txt', 'test2.txt']
    send_email(from_addr, password, to_addr, subject, html_content, attachment_files)

# import smtplib
# from email.mime.text import MIMEText
# from email.mime.multipart import MIMEMultipart
# from email.mime.base import MIMEBase
# from email import encoders
# import os
#
# aaa SendingEmails:
#     # 初始化方法
#     def __init__(self, email_address, password, receiver_email, subject, message):
#         self.email_address = email_address  # 邮箱地址
#         self.password = password  # 邮箱密码
#         self.receiver_email = receiver_email  # 接收邮件的邮箱地址
#         self.subject = subject  # 邮件主题
#         self.message = message  # 邮件内容
#
#     # 发送邮件的方法
#     def send_email(self):
#         try:
#             # 创建邮件对象
#             msg = MIMEMultipart()
#             msg['From'] = self.email_address
#             msg['To'] = self.receiver_email
#             msg['Subject'] = self.subject
#
#             # 添加邮件正文，指定编码为UTF-8
#             msg.attach(MIMEText(self.message, 'plain', 'utf-8'))
#
#             # 连接到SMTP服务器并发送邮件
#             server = smtplib.SMTP('smtp.qq.com', 587)
#             server.starttls()
#             server.login(self.email_address, self.password)
#             server.sendmail(self.email_address, self.receiver_email, msg.as_string())
#             server.quit()
#             print("邮件发送成功！")
#         except Exception as e:
#             print("邮件发送失败！", str(e))
#
#     # 发送带有附件的邮件
#    # 发送带有附件的邮件
#     def send_email_with_attachment(self, attachment_path):
#         try:
#             # 检查文件是否存在
#             if not os.path.isfile(attachment_path):
#                 print(f"文件 {attachment_path} 不存在！")
#                 return
#
#             # 创建邮件对象
#             msg = MIMEMultipart()
#             msg['From'] = self.email_address
#             msg['To'] = self.receiver_email
#             msg['Subject'] = self.subject
#
#             # 添加邮件正文，指定编码为UTF-8
#             msg.attach(MIMEText(self.message, 'plain', 'utf-8'))
#
#             # 添加附件
#             with open(attachment_path, 'rb') as file:
#                 part = MIMEBase('application', 'octet-stream')  # 创建一个MIMEBase对象
#                 part.set_payload(file.read())
#                 encoders.encode_base64(part)
#                 part.add_header('Content-Disposition', f'attachment; filename="{os.path.basename(attachment_path)}"')
#                 msg.attach(part)
#
#             # 连接到SMTP服务器并发送邮件
#             server = smtplib.SMTP('smtp.qq.com', 587)
#             server.starttls()
#             server.login(self.email_address, self.password)
#             server.sendmail(self.email_address, self.receiver_email, msg.as_string())
#             server.quit()
#             print("邮件发送成功！")
#         except Exception as e:
#             print("邮件发送失败！", str(e))
#
#
# if __name__ == '__main__':
#     email_address = '1228912646@qq.com'
#     password = 'puxutzlcndajijea'
#     receiver_email = '1228912646@qq.com'
#     subject = '测试邮件'
#     message = '这是一封测试邮件。'
#     send_email = SendingEmails(email_address, password, receiver_email, subject, message)  # 创建一个SendingEmails对象
#     # send_email.send_email()
#     attachment_path=r'qq邮箱授权码.txt'
#     send_email.send_email_with_attachment(attachment_path)
